
var updateId = JSON.parse(localStorage.getItem('user')).userId;

var $,
    table,
    layer,
    form;

layui.use(['jquery', 'table', 'layer', 'form'], function () {
    $ = layui.jquery,
        table = layui.table,
        form = layui.form,
        layer = layui.layer;

    getDeptData('', '', '', '');


    form.on('switch(fil)', function (data) {
        var name = data.value;
        var id = name.substring(0, name.lastIndexOf('-'));
        name = name.substring(name.lastIndexOf('-') + 1);
        if (data.elem.checked){
            layer.open({
                type: 0,
                content: '您确定要启用' + name + '吗？',
                area: ['300px', '190px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    layer.msg('启用成功', {icon: 6});
                    updateStatus(id, 1)
                },
                btn2:function (index, layero) {
                    layer.msg('启用取消', {icon: 5});
                    $('input[value='+data.value+']').siblings('.layui-unselect').removeClass('layui-form-onswitch');
                    data.elem.checked = false;
                }
            });
        }else {
            layer.open({
                type: 0,
                content: '您确定要停用' + name + '吗？',
                area: ['300px', '190px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    layer.msg('停用成功', {icon: 5});
                    updateStatus(id, 2)
                },
                btn2:function (index, layero) {
                    layer.msg('停用取消', {icon: 6});
                    $('input[value='+data.value+']').siblings('.layui-unselect').addClass('layui-form-onswitch');
                    data.elem.checked = true;
                }
            });
        }
        function updateStatus(id, status){
            $.ajax({
                url: '/Dept/updateDept',
                type: 'post',
                data: {
                    'id': id,
                    'updateId': updateId,
                    'status': status,
                },
                dataType: 'json',
            });
        }
    });
})

//获取部门数据
function getDeptData(deptName, createName, createDate, status){
    $.ajax({
        url: '/Dept/getAllDept',
        type: 'post',
        data: {
            'deptName': deptName,
            'createName': createName,
            'createDate': createDate,
            'status': status,
        },
        dataType: 'json',
        success: function (item) {
            getDeptTable(item.data);
        }
    });
};

//部门数据表
function getDeptTable(data) {
    table.render({
        elem: '#demo',
        data: data,
        toolbar: true,
        cols:[[
            {type: 'checkbox'},
            {title:'序号', align:'center', templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'deptName',title:'部门名称', align:'center'},
            {field:'deptDescirbe',title:'部门描述',align:'center'},
            {field:'createName',title:'创建人',align:'center'},
            {field:'createDate',title:'创建时间',align:'center'},
            {field:'status',title:'状态',align:'center', templet: function (data) {
                    if (data.status == 1){
                        return '<input type="checkbox" name="switch" value="'+data.id+'-'+data.deptName+'" lay-filter="fil" lay-text="启用 | 停用" lay-skin="switch" checked>'
                    } else{
                        return '<input type="checkbox" name="switch" value="'+data.id+'-'+data.deptName+'" lay-filter="fil" lay-text="启用 | 停用" lay-skin="switch">'
                    }

                }},
            {title:'操作',align:'center', templet:function(data){
                    return "<a href='JavaScript:updateDept(\"" + data.id +  "\");' class='operation'>编辑</a>" + '&nbsp;&nbsp;'
                        +"<a class='operation' href='javascript:deleteDept(\"" + data.id + "\", \"" + data.productName + "\");'>删除</a>";
                }},
        ]]
    });
}

//搜索
function search() {
    var deptName = $('input[name=deptName]').val();
    var createName = $('input[name=createName]').val();
    var createDate = $('input[name=createDate]').val();
    var status = $('select[name=status]').val();

    if (createName != null && createName != ''){
        $.ajax({
            url: '/User/getByName',
            type: 'post',
            data: {'createName': createName},
            dataType: 'json',
            async: false,
            success:function (item) {
                createName = item.data;
            }
        });
    }

    getDeptData(deptName, createName, createDate, status);
}

//添加部门信息
function addDept() {
    layer.open({
        type: 2,
        content: 'layer-authority-addDepartment.html?tx=',
        shade:0.4,
        title:'人员信息表',
        offset: 'auto',
        maxmin: true,
        area: ['690px', '500px'],
        btn: ['添加', '取消'],
        yes: function (index, layero) {
            var form = layero.find('iframe').contents();
            var deptName = form.find('input[name=deptName]').val();
            var status = form.find('select[name=status]').val();
            var createDate = form.find('input[name=createDate]').val();
            var deptDescirbe = form.find('textarea[name=deptDescirbe]').val();
            var createId = JSON.parse(localStorage.getItem('user')).userId;
            $.ajax({
                url: '/Dept/addDept',
                type: 'post',
                data: {
                    'deptName': deptName,
                    'status': status,
                    'createId': createId,
                    'createDate': createDate,
                    'deptDescirbe': deptDescirbe,
                },
                dataType: 'json',
                success: function (item) {
                    if (item.data){
                        layer.msg('添加成功');
                        layer.close(index);
                        getDeptData('', '', '', '');
                    } else{
                        layer.msg('添加失败');
                    }
                }
            });
        }
    });
};

//修改部门信息
function updateDept (id) {
    layer.open({
        type: 2,
        content: 'layer-authority-updateDepartment.html?tx=' + encodeURI(id),
        shade:0.4,//设置遮罩
        title:'人员信息表',
        offset: 'auto',
        maxmin: true,
        area: ['690px', '500px'],
        btn: ['编辑', '取消'],
        yes: function (index, layero) {
            var form = layero.find('iframe').contents();
            var deptName = form.find('input[name=deptName]').val();
            var deptDescirbe = form.find('textarea[name=deptDescirbe]').val();
            $.ajax({
                url: '/Dept/updateDept',
                type: 'post',
                data: {
                    'id': id,
                    'deptName': deptName,
                    'updateId': updateId,
                    'deptDescirbe': deptDescirbe,
                },
                dataType: 'json',
                success: function (item) {
                    if (item.data){
                        layer.msg('编辑成功');
                        layer.close(index);
                        getDeptData('', '', '', '');
                    } else{
                        layer.msg('编辑失败');
                    }
                }
            });
        }
    });
};

//根据id删除部门信息
function deleteDept (id) {
    $.ajax({
        url: '/Dept/deleteDept',
        type: 'post',
        data: {
            'id': id,
        },
        dataType: 'json',
        success: function (item) {
            if (item.data){
                layer.msg('删除成功');
                getDeptData('', '', '', '');
            } else{
                layer.msg('删除失败');
            }
        }
    });
}

//刷新
function refresh () {
    location.reload();
}